package threadDemo;

/**
 * @Author: Jie
 * @Date: 2019/6/4 18:58
 * @Function :
 */
public class ThreadSleep_Interrupt {
    public static void main(String[] args) {

        Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {
                while (!Thread.currentThread().isInterrupted()) {

                    //
                    try {
                        System.out.println("new Object().wait start");
                        new Object().wait(2000);//响应 threadA.interrupt(), java.lang.IllegalMonitorStateException
                    } catch (Exception e) {
                        System.out.println("Object().wait InterruptedException--->");
                        e.printStackTrace();
                    }
                    try {
                        System.out.println("T start sleep");
                        Thread.sleep(2000);//响应 threadA.interrupt() ,java.lang.InterruptedException
                        System.out.println("T over sleep");
                    } catch (InterruptedException e) {
                        System.out.println("Thread.sleep InterruptedException--->");
                        e.printStackTrace();
                        //TODO 解决 Thread.currentThread().isInterrupted() 重置标志位的问题。
                        Thread.currentThread().interrupt();
                    }
                }
            }
        });
        threadA.start();

        new Thread(new Runnable() {
            @Override
            public void run() {
//                thread.start();
                try {
                    Thread.sleep(1000);
                    threadA.interrupt();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
